/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package base;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tue
 */
public class Utilities {
    public static boolean debugMode = true;
    public static boolean MULTI_THREAD = false;


    private final static List<String> allAddresses = new ArrayList<String>();

    private static String MAC;

    static {
        try {
            Enumeration<NetworkInterface> ifc = NetworkInterface.getNetworkInterfaces();
            while(ifc.hasMoreElements()){
                NetworkInterface itf = ifc.nextElement();
                if(itf.isUp()){
                    Enumeration<InetAddress> eAddr = itf.getInetAddresses();
                    while(eAddr.hasMoreElements()){
                        InetAddress address = eAddr.nextElement();
                        allAddresses.add(address.getHostAddress());
                    }
                }
            }
        } catch (SocketException socketEx){
            System.err.println("ERROR: " + socketEx.getMessage());
        }

        try {
            Enumeration e = NetworkInterface.getNetworkInterfaces();
            while(e.hasMoreElements()){
                NetworkInterface ifc = (NetworkInterface) e.nextElement();
                if(ifc.isUp()) {
                    byte [] mac = ifc.getHardwareAddress();
                    if(mac != null){
                        MAC = "";
                        for (int i = 0; i < mac.length; i++){
                            MAC += String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
                        }
                       if(MAC.trim().length() > 0) break;
                    }
                }
            }
        } catch(SocketException socketEx){
            MAC = "ERROR";
        }
    }


    public static void TPPrint(String str){
        if(debugMode) System.out.println("[T.P] " + str);
    }
    public static void TPPrint(Hashtable table){
        if(debugMode) TPPrint(table.toString());
    }
    public static void WPPrint(String str){
        if(debugMode) System.out.println("[W.P] " + str);
    }
    public static void WPPrint(Hashtable table){
        if(debugMode) WPPrint(table.toString());
    }
    public static void CPPrint(String str){
        if(debugMode) System.out.println("[C.P] " + str);
    }
    public static void CPPrint(Hashtable str){
        if(debugMode) CPPrint(str.toString());
    }
    public static void WIPrint(String msg){
        if(debugMode) System.out.println("[W.I] " + msg);
    }
    public static int linkNameToSign(String linkName){
        if(linkName.startsWith("+")) return 1;
        if(linkName.startsWith("-")) return -1;
        return 0;
    }
    public static String removeSignOfLink(String linkName){
        if(linkName.startsWith("+") || linkName.startsWith("-"))
            return linkName.substring(1);
        return linkName;
    }
    public static void delay(int minisec){
        try {
            Thread.sleep(minisec);
        } catch (InterruptedException ex) {
            Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static String getIp(){
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException ex) {
            Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "Unknown";
    }
    public static boolean isLocalHost(String ipAddress){        
        return allAddresses.contains(ipAddress);
    }
    public static String readWaveFile(String path) {
        String result = "";
        FileReader fileReader;
        try {
            fileReader = new FileReader(path);
            BufferedReader reader = new BufferedReader(fileReader);
            while(reader.ready()){
                String readed = reader.readLine();
                result += readed;
            }
        } catch (IOException ex) {
            result = "";
        }
        return result;
    }

    public static String getMACAddress(){
        return MAC;
    }
}
